package com.wuwu.springcloud.Controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wuwu.springcloud.pojo.Dept;
import com.wuwu.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 提供Restful 服务
@RestController
public class DeptController {
    @Autowired
     DeptService deptService;
    //获取一些配置信息 得到具体的微服务
    @Autowired
    private DiscoveryClient discoveryClient;


    @PostMapping("/dept/add")
     public boolean add(Dept dept){
          return deptService.addDept(dept);
     }
@HystrixCommand(fallbackMethod = "Hystrixget")
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.queryBuId(id);
     }
     //备份
    public Dept Hystrixget(@PathVariable("id") Long id){
        return new Dept().setDeptno(id).setDname("没有服务").setDb_source("没有数据库") ;
    }


     @GetMapping("/dept/list")
    public List<Dept> queryAll(){
        return deptService.queryList();
    }

    @GetMapping("/dept/discovery")
    public Object discovery(){
        // 获取 所有微服务清单
         List<String> Service = discoveryClient.getServices();
         System.out.println("discovery------"+Service);
         //得到具体的一个微服务信息    id
         List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
         for (ServiceInstance instance : instances) {
             //host 主机名称  port  端口号  url 地址
             System.out.println(
                     instance.getHost()+"\t"+
                     instance.getPort()+"\t"+
                     instance.getUri()+"\t"+
                     instance.getServiceId()+"\t"

             );
         }
         return this.discoveryClient;
     }
}
